<?php

namespace App\Models;

use Carbon\Carbon;
use Hyn\Tenancy\Traits\UsesTenantConnection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Lrental\FeeFormula\HasPrice;

/**
 * App\Models\AssetStock
 *
 * @property int $id
 * @property string $name
 * @property string|null $product_model_number
 * @property string|null $description
 * @property int $init_quantity
 * @property int $available_quantity
 * @property int $total_quantity
 * @property int $checkout_quantity
 * @property float $marking_price_per_unit
 * @property float $bid_price_per_unit
 * @property int|null $category_id
 * @property int|null $sub_category_id
 * @property int $affiliation_id 归属
 * @property int|null $brand_id
 * @property int $keeper_id 保管人
 * @property int|null $location_id
 * @property int|null $vendor_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\AssetAffiliation $affiliation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Attachment[] $attachments
 * @property-read int|null $attachments_count
 * @property-read \App\Models\AssetBrand|null $brand
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AssetBundleItem[] $bundle
 * @property-read int|null $bundle_count
 * @property-read \App\Models\AssetCategory|null $category
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AssetStockChangeHistory[] $changeHistories
 * @property-read int|null $change_histories_count
 * @property-read mixed $price
 * @property-read mixed $type
 * @property-read \App\Models\AssetKeeper $keeper
 * @property-read \App\Models\AssetLocation|null $location
 * @property-read \App\Models\AssetCategory|null $subCategory
 * @property-read \App\Models\AssetVendor|null $vendor
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock fuzzyFilter($queryString)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\AssetStock onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereAffiliationId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereAvailableQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereBidPricePerUnit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereBrandId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereCheckoutQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereInitQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereKeeperId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereLocationId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereMarkingPricePerUnit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereProductModelNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereSubCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereTotalQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AssetStock whereVendorId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\AssetStock withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\AssetStock withoutTrashed()
 * @mixin \Eloquent
 */
class AssetStock extends Model implements HasPrice
{
    use SoftDeletes, UsesTenantConnection;
    protected $guarded = [];
    protected $with = ['affiliation', 'category', 'subCategory', 'brand', 'keeper', 'location', 'vendor'];
    protected $appends = ['type'];

    public function getTypeAttribute() {
        return self::class;
    }

    public function category()
    {
        return $this->belongsTo(AssetCategory::class);
    }

    public function subCategory()
    {
        return $this->belongsTo(AssetCategory::class, 'sub_category_id');
    }

    public function affiliation()
    {
        return $this->belongsTo(AssetAffiliation::class);
    }

    public function brand()
    {
        return $this->belongsTo(AssetBrand::class);
    }

    public function keeper()
    {
        return $this->belongsTo(AssetKeeper::class);
    }

    public function location()
    {
        return $this->belongsTo(AssetLocation::class);
    }

    public function vendor()
    {
        return $this->belongsTo(AssetVendor::class);
    }

    public function bundle()
    {
        return $this->morphMany(AssetBundleItem::class, "bundleable");
    }

    public function changeHistories()
    {
        return $this->hasMany(AssetStockChangeHistory::class);
    }

    public function scopeFuzzyFilter($query, $queryString)
    {
        $likeStr = '%' . $queryString . '%';
        $brandIds = AssetBrand::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $affiliationIds = AssetAffiliation::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $keeperIds = AssetKeeper::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $vendorIds = AssetVendor::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $locationIds = AssetLocation::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $categoryIds = AssetCategory::where('name', 'like', $likeStr)->select(['id'])->get()->pluck(['id']);
        $query->where('name', 'like', $likeStr)
            ->orWhere('product_model_number', 'like', $likeStr)
            ->orWhere('marking_price_per_unit', 'like', $likeStr)
            ->orWhere('bid_price_per_unit', 'like', $likeStr)
            ->orWhere('init_quantity', 'like', $likeStr)
            ->orWhere('available_quantity', 'like', $likeStr)
            ->orWhere('checkout_quantity', 'like', $likeStr)
            ->orWhere('total_quantity', 'like', $likeStr)
            ->orWhereIn('category_id', $categoryIds)
            ->orWhereIn('sub_category_id', $categoryIds)
            ->orWhereIn('brand_id', $brandIds)
            ->orWhereIn('affiliation_id', $affiliationIds)
            ->orWhereIn('keeper_id', $keeperIds)
            ->orWhereIn('vendor_id', $vendorIds)
            ->orWhereIn('location_id', $locationIds);
        try {
            if (Carbon::createFromFormat(config("app.date_format"), $queryString) != false) {
                $query->orWhere('bid_date', $queryString);
            }
        } catch (\Exception $e) {
        }
        return $query;
    }

    public function attachments()
    {
        return $this->morphMany(Attachment::class, 'attachable');
    }

    public function getPriceAttribute()
    {
        return $this->attributes['marking_price_per_unit'];
    }
}
